package com.txq.bilibili.app.tasks;

import android.os.AsyncTask;
import com.txq.bilibili.app.interfaces.SocialFragmentInterface;
import com.txq.bilibili.app.utils.HttpTool;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by yun on 2015/3/23.
 */
public class SocialTask extends AsyncTask<String,Void,JSONObject> {

    private SocialFragmentInterface socialFragmentInterface;

    public  SocialTask(SocialFragmentInterface socialFragmentInterface){
        this.socialFragmentInterface=socialFragmentInterface;
    }
    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject ret=null;

        if(params!=null && params.length>0){
            String url=params[0];
            byte [] bytes= HttpTool.get(url);
            String str=null;
            try {
                if(bytes!=null) {
                    url=new String(bytes,"UTF-8");
                    ret = new JSONObject(url);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {

        if(jsonObject!=null) {
            socialFragmentInterface.parseJson(jsonObject);
        }

    }
}
